﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using EZCRS.HCMS.Data;
using EZCRS.HCMS.Entity;

namespace EZCRS.HCMS.SqlClient
{
    public class sql_AdmGroup
    {
        public string SqlStr = "", ErrMsg = "";

       
        /// <summary>
        /// 集团列表
        /// </summary>
        /// <returns></returns>
        public DataTable Group_List(string GroupList)
        {
            #region 独立
            //if (GroupList != "")
            //    SqlStr = "select * from hfg_group where gid in (" + CFun.VerifySQL(GroupList) + ") order by gid desc";
            //else
            //    SqlStr = "select * from hfg_group order by gid desc";

            //return CommonDataAccess.GetTable(SqlStr, "tab");
            #endregion

            #region hubs原有集团集成
            if (GroupList != "")
                SqlStr = "select * from HotelGroup where ClassId in (" + GroupList + ") order by parentid,orderid ";
            else
                SqlStr = "select * from HotelGroup where ParentId<>0 order by parentid,orderid ";

            return ExecSql.GetDataSet(SqlStr).Tables[0];
            #endregion
        }
        public DataTable Group_List(int parentid)
        {
            return ExecSql.GetDataSet("select * from hotelgroup where parentid=" + parentid.ToString() + " order by parentid,orderid").Tables[0];
        }

        public List<dropdowndata> Group_list()
        {
            List<dropdowndata> _list = new List<dropdowndata>();
            DataTable dt = ExecSql.GetDataSet("select * from HotelGroup  order by parentid,orderid ").Tables[0];
            _list = simgroup(_list, dt, null, "");

            _list.Add(new dropdowndata("0", "第一级栏目"));
            return _list;
        }

        private List<dropdowndata> simgroup(List<dropdowndata> _list, DataTable dt, DataRow cdr, string fspace)
        {
            DataRow[] drlist;

            string appstr = "├  ";
            string appstrend = "└  ";
            if (fspace == "")
            {
                appstr = "";
                appstrend = "";
            }
            else
            {
                appstr = fspace + appstr;
                appstrend = fspace + appstrend;
            }
            if (cdr == null)
                drlist = dt.Select("");
            else
                drlist = dt.Select(" parentid=" + cdr["classid"].ToString());

            foreach (DataRow drone in drlist)
            {
                if ((fspace == "" && drone["parentid"].ToString() == "0") || (fspace != "" && drone["parentid"].ToString() != "0"))
                {
                    if (drone == drlist[drlist.Length - 1])
                        _list.Add(new dropdowndata(drone["classid"].ToString(), appstrend + drone["classname"].ToString()));
                    else
                        _list.Add(new dropdowndata(drone["classid"].ToString(), appstr + drone["classname"].ToString()));
                    simgroup(_list, dt, drone, fspace + " ");
                }
            }
            return _list;
        }



        /// <summary>
        /// 添加或修改集团信息
        /// </summary>
        /// <param name="GId">集团编号；重复为修改，否则新增</param>
        /// <param name="GIdE">英文编号</param>
        /// <param name="GrpName">集团名称</param>
        /// <param name="GrpDomain">集团域名</param>
        /// <param name="InUser">录入人</param>
        /// <param name="InDate">录入时间</param>
        /// <param name="Msg">修改或新增</param>
        /// <returns></returns>
        public bool Group_Add(string GId, string GIdE, string GrpName, string GrpDomain, string InUser, out string Msg)
        {
            if (GIdE == "")
                GIdE = "0";
            DataTable dt = ExecSql.GetDataSet("select * from hfg_group where gid=" + GId + "").Tables[0];
            if (dt.Rows.Count > 0)
            {
                Msg = "修改";
                SqlStr = "update hfg_group set gid_e=" + GIdE + ",grpname=@grpname,grpdomain=@grpdomain where gid=" + GId + "";
            }
            else
            {
                Msg = "新增";
                //SqlStr = "insert into hfg_group(gid,gid_e,grpname,grpdomain,inuser,indate) values("+GId+"," + GIdE + ",@grpname,@grpdomain,@inuser,@indate)";
                SqlStr = "insert into hfg_group(gid_e,grpname,grpdomain,inuser,indate) values(" + GIdE + ",@grpname,@grpdomain,@inuser,@indate)";
            }

            SqlParameter[] pam = new SqlParameter[4];
            pam[0] = new SqlParameter("@grpname", GrpName);
            pam[1] = new SqlParameter("@grpdomain", GrpDomain);
            pam[2] = new SqlParameter("@inuser", InUser);
            pam[3] = new SqlParameter("@indate", DateTime.Now.ToString());

            if (ExecSql.SqlExecNoquery(SqlStr, pam) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 删除集团
        /// </summary>
        /// <param name="GId">集团编号</param>
        /// <returns></returns>
        public bool Group_Del(string GId)
        {
            #region 独立
            //if (CommonDataAccess.Delete("delete from hfg_group where gid="+CFun.VerifySQL(GId)+"",out ErrMsg))
            //    return true;
            //else
            //    return false;
            #endregion

            #region 集成hubs1
            if (ExecSql.SqlExecNoquery("delete from HotelGroup where ClassId=" + GId + "") > 0)
                return true;
            else
                return false;
            #endregion
        }


        /// <summary>
        /// 返回一条集团信息
        /// </summary>
        /// <param name="GId">集团编号</param>
        /// <returns></returns>
        public DataRow Group_Item(string GId)
        {
            //return CommonDataAccess.GetTable("select * from hfg_group where gid="+CFun.VerifySQL(GId)+"", "tab").Rows[0];

            return ExecSql.GetDataSet("select * from HotelGroup where ClassId=" + GId + "").Tables[0].Rows[0];
        }


        public bool Group_Add(string gclass, string gname, string gnameen, string greadme, string gcity, string gshowtop, string gcss, string gid)
        {
            string sqlstr = "";
            if (gid == "0")
                sqlstr = "insert into hotelgroup(classname,classname_e,parentid,readme,iselite,showontop) values(@classname,@classname_e,@parentid,@readme,@iselite,@showontop)";
            else
                sqlstr = "update hotelgroup set classname=@classname,classname_e=@classname_e,parentid=@parentid,readme=@readme,iselite=@iselite,showontop=@shownotop where classid=" + gid + "";
            SqlParameter[] pam = new SqlParameter[6];
            pam[0] = new SqlParameter("@classname", gname);
            pam[1] = new SqlParameter("@classname_e", gnameen);
            pam[2] = new SqlParameter("@parentid", gclass);
            pam[3] = new SqlParameter("@readme", greadme);
            pam[4] = new SqlParameter("@iselite", gcity);
            pam[5] = new SqlParameter("@showontop", gcss);

            if (ExecSql.SqlExecNoquery(sqlstr, pam) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 保存排序设定
        /// </summary>
        /// <param name="classidlist"></param>
        /// <returns></returns>
        public bool Group_SaveSort(string classidlist)
        {
            SqlStr = "";
            string[] idlist = classidlist.Split(',');
            int i = 0;
            foreach (string strid in idlist)
            {
                i++;
                SqlStr += "update hotelgroup set orderid=" + i.ToString() + " where classid=" + strid + ";";
            }

            if (ExecSql.SqlExecNoquery(SqlStr) > 0)
                return true;
            else
                return false;
        }

        
    }
}
